package com.qdev.apps.core.setup.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * a simple encoding filter, use "UTF-8" as default encoding.
 * @author Quake Wang
 * @since 2004-7-19
 * @version $Revision: 1.1 $
 */
public class EncodingFilter implements Filter {
    private String defaultEncoding = "UTF-8";

    public void init(FilterConfig config) throws ServletException {
        if(config.getInitParameter("defaultEncoding") != null){
            this.defaultEncoding = config.getInitParameter("defaultEncoding");
        }
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
            ServletException {
        req.setCharacterEncoding(getEncoding());
        res.setContentType(getContentType());
        chain.doFilter(req, res);
    }

    private String getEncoding() {
        return defaultEncoding;
    }

    protected String getContentType() {
        return "text/html; charset=" + getEncoding();
    }

    public void destroy() {

    }

}